﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2014-6-11
/// Purpose: 		有缓存的以Id为Key记录的集合类定义文件
/// ****************************************************************************
/// Modify By		Date			Remark
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RB.Domain.Models;

namespace RB.Domain.RelationalMapping
{
    public class CachedIdKeyedRecordSet<TItem, TKey> : CachedKeyedRecordSet<TItem, TKey>
        where TItem : class, IIdKeyed<TKey>
    {
        protected override TItem GetValue(TKey key)
        {
            return base.SingleOrDefault(t => t.Id.Equals(key));
        }

        protected override void SetValue(TKey key, TItem value)
        {
            base.Update(p => value, p => p.Id.Equals(key));
        }

        protected override TKey GetKey(TItem item)
        {
            return item.Id;
        }

        protected override bool RemoveByKey(TKey key, DataAccess.Executor exec)
        {
            int n = base.RemoveAll(p => p.Id.Equals(key), exec);
            return n > 0;
        }
    }
}
